import _ from 'lodash'
import {isRef, reactive, toRefs} from "vue";
// 空内容时显示的内容
export const nothingTip = '-'
/**
 *
 * @param emptyText 空的时候显示
 * @param valueHandler 处理cell格式化用
 * @returns {function(*, *, *, *): *}
 */
export function useIfEmptyForTableFormatter(
    emptyText = nothingTip,
    valueHandler = value => value
) {
    return (row, column, cellValue, index) =>
        isEmpty(cellValue) ? emptyText : valueHandler(cellValue)
}

/**
 * 竞赛报备表单审核状态(学生)
 * @returns
 */
export function cptStatusFormat(
    emptyText = nothingTip,
) {
    let map= new Map()
    map.set(0,'待初审')
    map.set(1,'初审通过,待上传材料')
    map.set(2,'初审未通过')
    map.set(3,'未获奖，已结束')
    map.set(4,'认定中，待审核')
    map.set(5,'认定成功')
    map.set(6,'认定失败，需重传材料')

    return (row, column, cellValue, index) =>
        isEmpty(cellValue) ? emptyText : map.get(cellValue)
}

/**
 * 竞赛报备表单审核状态(admin)
 * @returns
 */
export function cptStatusFormat_admin(
    emptyText = nothingTip,
) {
    let map= new Map()
    map.set(0,'待初审')
    map.set(1,'已审核')
    map.set(2,'初审未通过')
    map.set(3,'已结束')
    map.set(4,'待认定')
    map.set(5,'认定成功')
    map.set(6,'认定失败，待重传材料')

    return (row, column, cellValue, index) =>
        isEmpty(cellValue) ? emptyText : map.get(cellValue)
}

/**
 * 参与方式
 * @param emptyText
 * @returns {function(*, *, *, *): string|any}
 */
export function cptPartTypeFormat(
    emptyText = nothingTip,
) {
    let map= new Map()
    map.set(0,'个人')
    map.set(1,'团队')

    return (row, column, cellValue, index) =>
        isEmpty(cellValue) ? emptyText : map.get(cellValue)
}


/**
 * 竞赛方式
 * @param emptyText
 * @returns {function(*, *, *, *): string|any}
 */
export function cptPartModeFormat(
    emptyText = nothingTip,
) {
    let map= new Map()
    map.set(0,'线上')
    map.set(1,'线下')

    return (row, column, cellValue, index) =>
        isEmpty(cellValue) ? emptyText : map.get(cellValue)
}

/**
 * 竞赛日期格式化
 */
export function cptDateFormat(
    emptyText = nothingTip,
) {
    return (row, column, cellValue, index) =>
        isEmpty(cellValue) ? emptyText : cellValue.substring(0, 10)

}

/**
 * 判断是否为空
 * 1. 数字 → false
 * 2. 函数 → false
 * 3. 'null'、'undefined → true
 * 4. lodash.isEmpty(value)
 * @param value 值
 * @returns {boolean} 是否为空
 */
export function isEmpty(value) {
    return (
        !_.isNumber(value) && !_.isFunction(value) &&
        (_.isEmpty(value) || value === 'null' || value === 'undefined')
    )
}

/**
 * 竞赛审核状态option
 * @returns {{label: *, value: *}[]}
 */
export function cptStatusDict() {
    let statusDict = []

    let map= new Map()
    map.set(0,'待初审')
    map.set(1,'初审通过')
    map.set(2,'初审未通过')
    map.set(3,'未获奖，已结束')
    map.set(4,'认定中，待审核')
    map.set(5,'认定成功')
    map.set(6,'认定失败，需重传材料')

    statusDict = Array.from(map).map(([key, value]) => ({
        label: value,
        value: key
    }));

    console.log("dict:" ,statusDict)
    return statusDict
}

/**
 * 竞赛审核状态option(admin
 * @returns {{label: *, value: *}[]}
 */
export function cptStatusDict_admin() {
    let statusDict = []

    let map= new Map() //key，value
    map.set(0,'待初审')
    map.set(1,'已审核')
    map.set(2,'初审未通过')
    map.set(3,'已结束')
    map.set(4,'待认定')
    map.set(5,'认定成功')
    map.set(6,'认定失败，待重传材料')

    statusDict = Array.from(map).map(([key, value]) => ({
        label: value,
        value: key
    }));

    console.log("dict:" ,statusDict)
    return statusDict
}

/**
 * 竞赛方式dict 线上/线下
 * @returns {{label: *, value: *}[]}
 */
export function getPartModeDict() {
    let dict = []

    let map= new Map()
    map.set(0,'线上')
    map.set(1,'线下')

    dict = Array.from(map).map(([key, value]) => ({
        label: value,
        value: key
    }));

    console.log("partModeDict:" ,dict)
    return dict
}

/**
 * 参与方式dict 个人/团队
 * @returns {{label: *, value: *}[]}
 */
export function getPartTypeDict() {
    let dict = []

    let map= new Map()
    map.set(0,'个人')
    map.set(1,'团队')

    dict = Array.from(map).map(([key, value]) => ({
        label: value,
        value: key
    }));

    console.log("partTypeDict:" ,dict)
    return dict
}

/**
 * 奖项级别option
 * @returns {{label: *, value: *}[]}
 */
export function getCognizanceLevelDict() {
    let dict = []

    let map= new Map()
    map.set(0,'国家级')
    map.set(1,'省部级')
    map.set(2,'校级')

    dict = Array.from(map).map(([key, value]) => ({
        label: value,
        value: key
    }));

    console.log("CognizanceLevelDict:" ,dict)
    return dict
}

/**
 * 奖项级别option
 * @returns {{label: *, value: *}[]}
 */
export function getCognizanceRankDict() {
    let dict = []

    let map= new Map()
    map.set(0,'特等奖')
    map.set(1,'一等奖')
    map.set(2,'二等奖')
    map.set(3,'三等奖')
    map.set(4,'优胜奖')

    dict = Array.from(map).map(([key, value]) => ({
        label: value,
        value: key
    }));

    console.log("CognizanceRankDict:" ,dict)
    return dict
}

/**
 * iso格式时间转为yyyy-mm-dd 00:00:00
 * @param isoString
 * @returns {string}
 */
export function convertISOToBasicString(isoString) {
    const date = new Date(isoString);
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    const hours = date.getHours().toString().padStart(2, '0');
    const minutes = date.getMinutes().toString().padStart(2, '0');
    const seconds = date.getSeconds().toString().padStart(2, '0');
    return `${year}-${month}-${day}`+' '+`${hours}:${minutes}:${seconds}`;
}

/**
 * 读取值
 * 1. 函数直接调用并返回值
 * 2. Ref获取.value值返回
 * 2. 其他直接返回
 * @param value
 * @returns {*} 值
 */
export const toValue = value => {
    if (_.isFunction(value)) {
        return value()
    } else if (isRef(value)) {
        return value.value
    } else {
        return value
    }
}